define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'project/type/index' + location.search,
                    add_url: 'project/type/add',
                    edit_url: 'project/type/edit',
                    del_url: 'project/type/del',
                    multi_url: 'project/type/multi',
                    import_url: 'project/type/import',
                    table: 'project_type',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        //{field: 'id', title: __('Id'), operate: 'LIKE'},
                        {field: 'project_name', title: __('Project_name'), operate: 'LIKE'},
                        {field: 'starttime', title: __('Starttime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'endtime', title: __('Endtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        //{field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        //{field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons:[
                                {
                                    name: 'add',
                                    text: '我要申报',
                                    title: '我要申报',
                                    classname: 'btn btn-success btn-add btn-dialog',
                                    icon: 'fa fa-plus',
                                    url: 'fastflow/bill/project/add',
                                    visible: function (row) {
                                        var timestamp = Date.parse(new Date());
                                        var times = timestamp/ 1000;
                                        //console.log(Config.rules);
                                        if(row['starttime'] <= times && row['endtime'] >= times && Config.rules > 0){
                                            return true;
                                        }
                                        else{
                                            return false;
                                        }
                                    }
                                },
                            ], formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
